package com.demo.testpackage;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.demo.entity.Student;

import java.util.*;
import java.util.stream.Collectors;

public class StreamTest {
    public static void main(String[] args) {
        // 快速创建Wrapper
        Wrappers.<Student>query().eq("id","001");

        StreamTest streamTest = new StreamTest();
        // 排序操作，先按照id排序，再按照那么排序
        List<Student> list = streamTest.buildList();
        list.stream().forEach(student -> System.out.println(student));
        System.out.println("--------------");
        list.stream().sorted(Comparator.comparing(Student::getId).thenComparing(Student::getName))
                .forEach(student -> System.out.println(student));

        // orElse：如果在当前条件下没有数据时，则进的操作
        List<Student> nullList1 = streamTest.buildNullList();
        Optional.ofNullable(nullList1).orElse(nullList1 = new ArrayList<>());
        System.out.println(nullList1);
        System.out.println();

        // 使用Option判断集合是否为null，结合stream使用，因为集合为null时直接调用stream方法会npe错误
        List<Student> nullList2 = streamTest.buildNullList();
        if (Optional.ofNullable(nullList2).isPresent()) {
            List<Student> collect = nullList2.stream().collect(Collectors.toList());
            System.out.println(collect.toString());
        } else {
            System.out.println("null");
        }
        System.out.println();

        /*
         Option.ofNullable(list)是判断当前集合是否在对内存中存在，并不是判断其是否有值
         若想判断一个集合即非空又有值的话则使用 list.size()>0 && list!=null
         */
        List<Student> nullList3 = streamTest.buildNullList();
        if (Optional.ofNullable(nullList3).isPresent()) {
            System.out.println("not null");
        } else {
            System.out.println("null");
        }
        List<Student> list1 = streamTest.buildList();
        if (list1 != null && list1.size() > 0) {
            System.out.println("该集合不为null，且有值");
        }
        System.out.println();

        // list集合为空值时，会自动跳过增强for循环；如果该集合为null则会报npe
        List<Student> nullList4 = streamTest.buildNullList();
        nullList4 = new ArrayList<>();
        boolean flag = true;
        for (Student student : nullList4) {
            flag = false;
        }
        if (flag) {
            System.out.println("测试成功，当list集合为空值时，或自动跳过增强for循环");
        } else {
            System.out.println("测试失败，当list集合为空值时，不会自动跳过增强for循环");
        }
        System.out.println();

        // 将list集合按条件过滤后返回map集合，(key1,key2)->key2)是为了避免key重复而制定的规则
        List<Student> list2 = streamTest.buildList();
        Map<String, Student> studentMap = list2.stream().filter(student -> student.getName().startsWith("z"))
                .collect(Collectors.toMap((a -> a.getId()), a -> a,(key1,key2)->key2));
        System.out.println();

        // 遍历map集合的几种方式
        System.out.println("第一种遍历方式：使用keyset");
        for (String s : studentMap.keySet()) {
            System.out.println("key:" + s + " value:" + studentMap.get(s));
        }
        System.out.println("第二种遍历方式：使用entryset");
        for (Map.Entry<String, Student> entry : studentMap.entrySet()) {
            System.out.println("key:" + entry.getKey() + " value:" + studentMap.get(entry.getKey()));
        }
        System.out.println("第三种遍历方式：使用iterator");
        Iterator<Map.Entry<String, Student>> iterator = studentMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Student> next = iterator.next();
            System.out.println("key:" + next.getKey() + " value:" + next.getValue());
        }
        System.out.println();

        /*
         将list集合过滤后返回list集合
         其中使用distinct()方法后，会将重复的数据筛出，其是按照数据内容比较重复，不是按照地址值
         */
        List<Student> list3 = streamTest.buildList();
        List<Student> list4 = list3.stream().distinct().filter(s -> s.getName().contains("z")).collect(Collectors.toList());
        list4.forEach(s-> System.out.println("将list集合过滤后返回list集合："+s));
        System.out.println();

        // map方法可以做为遍历集合使用，其实可直接省略map()方法，效果一样
        List<Student> list5 = streamTest.buildList();
        list5.stream().map(Student::getId).distinct().collect(Collectors.toList())
                .forEach(s -> System.out.println(s));
        System.out.println();

        // 遍历集合对其进行操作
        List<Student> list6 = streamTest.buildList();
        // 取任意一个
        Optional<Student> any =  list6.stream().findAny();
        System.out.println("任意取出集合中一个值（默认为第一个）："+any);
        // 统计集合元素个数
        long count = list6.stream().count();
        System.out.println("统计集合元素个数："+count);
        System.out.println();

        // 判断集合元素任意一个满足当前条件
        List<Student> list7 = streamTest.buildList();
        boolean flag2 = list7.stream().anyMatch(student1 -> student1.getName().contains("zs2"));
        System.out.println(flag2);
        // 判断集合中所有元素都要符合当前条件
        boolean flag3 = list7.stream().allMatch(s -> s.getName().contains("zs2"));
        System.out.println(flag3);
        System.out.println();

        // isPresent用于判断是否存在过滤后的元素，存在返回true，不存在返回false
        List<Student> list8 = streamTest.buildList();
        Optional<Student> first = list8.stream().filter(a -> "z2s2".equals(a.getName())).findFirst();
        if (first.isPresent()) {
            System.out.println("我是你爹:"+first.isPresent());
        } else {
            System.out.println("你是我儿:"+first.isPresent());
        }
        System.out.println();

        // ifPresent用与如果过滤后存在元素，则对其进行操作
        List<Student> list9 = streamTest.buildList();
        list9.stream().filter(a->"2".equals(a.getId())).findFirst().ifPresent(a->a.setName("zsssssssssss2"));
        list9.forEach(s-> System.out.println("使用ifPresent操作元素："+s));
    }

    /**
     * 创建非空集合
     *
     * @return
     */
    public List<Student> buildList() {
        List<Student> list = new ArrayList<>();

        Student stu11 = new Student();
        stu11.setId("1");
        stu11.setName("zs1");

        Student stu12 = new Student();
        stu12.setId("2");
        stu12.setName("zs2");

        Student stu13 = new Student();
        stu13.setId("3");
        stu13.setName("zs3");

        Student stu14 = new Student();
        stu14.setId("4");
        stu14.setName("zs4");

        Student stu15 = new Student();
        stu15.setId("4");
        stu15.setName("zs4");

        list.add(stu15);
        list.add(stu13);
        list.add(stu12);
        list.add(stu14);
        list.add(stu11);

        return list;
    }

    /**
     * 创建空集合
     *
     * @return
     */
    public List<Student> buildNullList() {
        List<Student> nullList = null;
        return nullList;
    }
}
